﻿using System.Reflection;
using CrawlerFramework.Repository;
using log4net;
using System;

namespace CrawlerFramework.Models
{
    public partial class Link
    {
        protected ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Update CrawlDate and CrawlStatus value before crawling
        /// </summary>
        public void BeginExecution()
        {
            Log.Info(string.Format("Begin execute link: {0} - id: {1}", Url, LinkId));

            using (var repo = new LinkRepository())
            {
                CrawlDate = DateTime.Now;
                CrawlStatus = CrawlStatus.Processing;
                repo.Edit(this);
                repo.Save();
            }
        }

        /// <summary>
        /// Update CrawlStatus after crawling finished
        /// </summary>
        public void EndExecution(bool isSuccess)
        {
            Log.Info(string.Format("Finished execute link: {0} - id: {1} - isSuccess: {2}", Url, LinkId, isSuccess));
            CrawlStatus = isSuccess ? CrawlStatus.Completed : CrawlStatus.Error;
            using (var repo = new LinkRepository())
            {
                repo.Edit(this);
                repo.Save();
            }
        }
    }
}
